
import 'dart:convert';
import 'dart:io';

import 'package:deviceinfo/deviceinfo.dart';
import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:massagist/models/logout_model_entity.dart';
import 'package:massagist/models/setting_model_entity.dart';
import 'package:massagist/models/user_info_model_entity.dart';

import '../models/acctravel_model_entity.dart';
import '../models/code_model_entity.dart';
import 'config.dart';

class Request {

  static bool isLocal = false;
  static String MAIN_INFO_URL = "https://iby.jiliol.cn/acctravel_info.json";
  static String LOGIN_URL = "http://byamv.jiliol.cn/sign/?a=index";
  static String WEB_PATH = "http://byamv.jiliol.cn/find/?";
  static String SETTING_URL = "http://byamv.jiliol.cn/sign/?a=my";
  static String ndgd = "";
  static Deviceinfo deviceinfo = Deviceinfo();

  static Future<List<AcctravelModelEntity>> requestMainInfo() async {
    if (isLocal) {
      String content = await rootBundle.loadString('assets/configs/acctravel_info.json');
      List<dynamic> jsonList = json.decode(content);
      return jsonList.map((json) => AcctravelModelEntity.fromJson(json)).toList();
    } else {
      final dio = Dio();
      final response = await dio.get(MAIN_INFO_URL, options: Options(responseType: ResponseType.plain));
      List items = json.decode(response.data);
      List<AcctravelModelEntity> models = items.map((user) => AcctravelModelEntity.fromJson(user)).toList();


      // var sp = await SharedPreferences.getInstance();
      // if (sp.get(TEMPConfig.BLACK_BOARD) != null) {
      //   print ("xxxxxxx");
      //   print (sp.getString(TEMPConfig.BLACK_BOARD));
      //   var resultArr = sp.getString(TEMPConfig.BLACK_BOARD)?.split("|");
      //   for (int i = 0; i < resultArr!.length; i++) {
      //     var uid = int.parse(resultArr[i]);
      //     print (uid);
      //     for (int j = 0; j < models.length; j++) {
      //       if (models[j].uid == uid) {
      //         models.removeAt(j);
      //         break;
      //       }
      //     }
      //   }
      // }

      return models;
    }
  }

  // static Future<List<MineItemModelEntity>> requestMineInfo() async {
  //   String jsonString = await rootBundle.loadString('asset/json/mine_info.json');
  //   List items = json.decode(jsonString);
  //   List<MineItemModelEntity> models = items.map((user) => MineItemModelEntity.fromJson(user)).toList();
  //
  //   return models;
  // }

  // static Future<String> requestPolicy(PolicyType policyType) async {
  //   String resultString = await rootBundle.loadString(policyType == PolicyType.USER ? "asset/txt/去按摩用户使用协议.md" : "asset/txt/去按摩隐私政策.md");
  //   return resultString;
  // }

  static Future<CodeModelEntity> requestCode(String phone) async {
    var infoMap = await getBasePara();
    infoMap?["mobile"] = phone;

    String resultString = convertPara(LOGIN_URL, infoMap!);
    var dio = new Dio();
    var response = await dio.post(resultString);
    return CodeModelEntity.fromJson(json.decode(response.toString()));
  }

  static Future<UserInfoModelEntity> requestCodeLogin(String phone, String code) async {
    var infoMap = await getBasePara();
    infoMap?["mobile"] = phone;
    infoMap?["code"] = code;

    String resultString = convertPara(LOGIN_URL, infoMap!);
    var dio = new Dio();
    var response = await dio.post(resultString);
    return UserInfoModelEntity.fromJson(json.decode(response.toString()));
  }

  static Future<UserInfoModelEntity> requestAutoPhone(jsonStr) async {
    var infoMap = await getBasePara();
    infoMap?["result"] = jsonStr;

    String resultString = convertPara(LOGIN_URL, infoMap!);
    var dio = new Dio();
    var response = await dio.post(resultString);
    return UserInfoModelEntity.fromJson(json.decode(response.toString()));
  }

  static Future<LogoutModelEntity> requestLogout() async {
    var infoMap = await getBasePara();
    infoMap?["key"] = Config.userInfo.key;

    String resultString = convertPara(LOGIN_URL, infoMap!);
    var dio = new Dio();
    var response = await dio.post(resultString);
    return LogoutModelEntity.fromJson(json.decode(response.toString()));
  }

  static Future<LogoutModelEntity> closeAccount() async {
    var infoMap = await getBasePara();
    infoMap?["key"] = Config.userInfo.key;
    infoMap?["cancel_member"] = "1";

    String resultString = convertPara(LOGIN_URL, infoMap!);
    var dio = new Dio();
    var response = await dio.post(resultString);
    return LogoutModelEntity.fromJson(json.decode(response.toString()));
  }

  static Future<String> requestWeb() async {
    var infoMap = await getBasePara();
    infoMap?["appname"] = "byamv";
    infoMap?["key"] = Config.userInfo.key;
    infoMap?["kefu"] = Config.userInfo.kefu;

    return convertPara(WEB_PATH, infoMap!);
  }

  static Future<SettingModelEntity> requestSetting() async {
    var infoMap = await getBasePara();
    try {
      infoMap?["key"] = Config.userInfo.key;
    } catch (e) {
      // infoMap?["key"] = "";
    }
    String resultString = convertPara(SETTING_URL, infoMap!);
    var dio = new Dio();
    var response = await dio.post(resultString);
    return SettingModelEntity.fromJson(json.decode(response.toString()));
  }

  static Future<Map?> getBasePara() async {
    if (Platform.isAndroid) {
      var infoMap = new Map();
      infoMap?["cersymn"] = "1.0.0";
      infoMap?["vayent"] = "iPhone";
      infoMap?["decyve"] = "iPhone8.1";
      infoMap?["ydfl"] = "ECC1E106-A47B-4AE9-AF41-D861E63870AE";
      infoMap?["decyve_tmken"] = "f00b89377314eb8abdd88802cdaa93db72812025d7bfb49ec3e4ba61829f7f7f";
      infoMap?["tdyd"] = "h10111c5633367e4f3c008630d5370f9a";
      infoMap?["ang"] = "116.123";
      infoMap?["alt"] = "33.123";
      infoMap?["alt"] = "33.123";
      infoMap?["signature"] = Config.cryptoClock();

      return infoMap;
    } else {
      var infoMap = await deviceinfo.getDeviceInfo();
      infoMap?["cersymn"] = "1.0.0";
      infoMap?["signature"] = Config.cryptoClock();
      infoMap?["tdyd"] = ndgd;
      infoMap?["ang"] = Config.lon.toString();
      infoMap?["alt"] = Config.lat.toString();
      infoMap?["appname"] = "byamv";

      return infoMap;
    }
  }

  static String convertPara(String url, Map para) {
    String resultString = url;
    print ("查看数据 ==== ");
    para.forEach((key, value) {
      resultString = resultString + "&" + key + "=" + value;
      print (key + " = " + value);
    });

    return resultString;
  }
}